package com.demo.cms.model;

import java.util.List;

import com.demo.Constants;
import com.demo.common.RegexUtils;
import com.demo.common.StringUtils;
import com.demo.core.model.Role;
import com.demo.core.model.Site;
import com.demo.core.model.support.NestedSetModel;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * Menu菜单模型
 * @author ryuu.kk
 *
 */
public class Navi extends NestedSetModel {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8314273796812697812L;

	/**
	 * navibar
	 */
	public static final String TYPE_NAVIBAR = "0";
	
	/**
	 * menu
	 */
	public static final String TYPE_MENU = "1";
	
	/**
	 * 站点ID
	 */
	private Long siteId;

	/**
	 * 频道ID
	 */
	private Long channelId;
	
	/**
	 * 栏目ID
	 */
	private Long categoryId;

	/**
	 * 文章ID
	 */
	private Long articleId;
	
	/**
	 * Menu名
	 */
	//private String name;
	
	/**
	 * 菜单标题
	 */
	private String title;
	
	/**
	 * Menu地址
	 */
	private String url;
	
	/**
	 * 选中条件
	 */
	private String pattern;
	
	/**
	 * 选中条件
	 */
	private String[] patterns;
	
	/**
	 * 菜单html的class
	 */
	private String css;
	
	/**
	 * 菜单样式
	 */
	private String style;
	/**
	 * 类型
	 */
	private String type;
	
	/**
	 * 数据状态
	 * R:可读;
	 * W:可改;
	 * D:可删;
	 */
	private String status;
	
	/**
	 * 可用标志
	 */
	private boolean enabled;
	
	/**
	 * 菜单允许匿名访问
	 * 默认菜单可匿名访问
	 */
	private boolean isAnonymous = false;
	
	/**
	 * 优先级排序
	 */
	private int priority;
	
	/**
	 * 频道地址
	 */
	private String channelPath;
	
	/**
	 * 栏目地址
	 */
	private String categoryPath;
	
	/**
	 * 文章地址
	 */
	private String articlePath;
	
	/**
	 * 菜单的访问权限列表
	 */
	private List<Role> roleList;

	/**
	 * 匹配当前地址是否菜单选中
	 * @param url String 
	 * @return boolean
	 */
	public boolean isActive(String base, String url) {
		// 获得当前线程中的Site对象
		Site site = MultisiteContextHolder.getSite();
		String suffix = "";
		if (site != null) {
			suffix = site.getSuffix();
			if (suffix.charAt(0) != '.') {
				suffix = '.' + suffix.trim();
			}
			suffix = suffix.toLowerCase();
		}
		if (StringUtils.isNotEmpty(pattern)) {
			boolean isActice;
			if (patterns == null) {
				patterns = pattern.split(",");
			}
			for (String pt : patterns) {
				isActice = RegexUtils.wildcardMatch(base + pt.trim(), url);
				if (isActice) {
					return true;
				}
			}
			return false;
		}
		pattern = base + '/' + this.channelPath;
		// 进行栏目匹配
		if (StringUtils.isNotEmpty(this.categoryPath)) {
			// 进行文章匹配
			if (StringUtils.isNotEmpty(this.articlePath)) {
				pattern += "/" + this.categoryPath + "/" + this.articlePath + "**";
				return RegexUtils.wildcardMatch(pattern, url);
			} else {
				pattern += "/" + this.categoryPath;
			}
		}
		// 增加URL后缀;
		pattern += suffix;
		return url.endsWith(pattern) || RegexUtils.wildcardMatch(pattern + "/**", url);
	}
	
	public boolean isActive(String url) {
		return isActive("/**", url);
	}
	
	public Long getSiteId() {
		return siteId;
	}

	public void setSiteId(Long siteId) {
		this.siteId = siteId;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getArticleId() {
		return articleId;
	}

	public void setArticleId(Long articleId) {
		this.articleId = articleId;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPage(String context, String suffix) {
		String page = "";
		if (StringUtils.isNotEmpty(this.url)) {
			if (url.charAt(0) == '/' && !url.startsWith(context)) {
				page = context + '/' + url;
			}
		} else {
			if (StringUtils.isNotEmpty(this.channelPath)) {
				page = context + '/' + this.channelPath;
				if (StringUtils.isNotEmpty(this.categoryPath)) {
					page += '/' + this.categoryPath;
					if (StringUtils.isNotEmpty(this.articlePath)) {
						page += '/' + this.articlePath;
					}
				} else {
					page += '/' + Constants.CMS_CHANNEL_DEFAULT;
				}
			}
		}
		// 判断是否存在扩展名的'.';
		if (StringUtils.isNotEmpty(suffix)) {
			if (suffix.charAt(0) != '.') {
				suffix += '.';
			}
		}
		return page + suffix;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public boolean isAnonymous() {
		return isAnonymous;
	}

	public void setAnonymous(Boolean isAnonymous) {
		this.isAnonymous = isAnonymous == null ? true : isAnonymous;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getChannelPath() {
		return channelPath;
	}

	public void setChannelPath(String channelPath) {
		this.channelPath = channelPath;
	}

	public String getCategoryPath() {
		return categoryPath;
	}

	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}

	public String getArticlePath() {
		return articlePath;
	}

	public void setArticlePath(String articlePath) {
		
		// 16进制转换
		if (articlePath != null) {
			this.articlePath = Long.toHexString(Long.parseLong(articlePath, 10));
		}
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
}
